package Server;



import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import Library.Reminder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Gui of the server
 * @author Maor
 *
 */
public class GUI_EchoServer extends JFrame implements WindowListener {
	
	private JTextField textField;
	private JTextField textField_1;
	private EchoServer es;
	private JTextField textField_2;
	private JPanel first;
	private JPanel sec;
	public  GUI_EchoServer forswitchPanels;
	
	/**
	 * constructor 
	 */
	public GUI_EchoServer() {
		
		getContentPane().setLayout(null);
		this.setSize(400, 300);
		this.setContentPane(getFirstPanel());
		this.setVisible(true);
		forswitchPanels=this;
		addWindowListener(this);
	}
		
	/**
	 * the first panel  that we show
	 * we put the deatils of the connection and 
	 * when we press connect he make connecting
	 * @return
	 */
	public JPanel getFirstPanel(){
		if (first == null) {
			first = new JPanel();
			first.setLayout(null);
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(58, 61, 63, 14);
		first.add(lblPort);
		
		JLabel lblUserName = new JLabel("User Name");
		lblUserName.setBounds(58, 96, 63, 14);
		first.add(lblUserName);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(58, 134, 63, 14);
		first.add(lblPassword);
		
		textField = new JTextField();
		textField.setBounds(164, 58, 129, 20);
		first.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(164, 93, 129, 20);
		first.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(163, 131, 130, 20);
		first.add(textField_2);
		textField_2.setColumns(10);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				es = new EchoServer(Integer.parseInt(textField.getText()),textField_1.getText(),textField_2.getText());
				//Thread t=new Thread(es);
				//t.start();
				try {
					es.listen();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			    Reminder reminder=new Reminder();
			    Thread t2=new Thread(reminder);
			    t2.start();
				forswitchPanels.setContentPane(sec());
				forswitchPanels.setVisible(true);
			}
		});
		btnConnect.setBounds(175, 181, 91, 23);
		first.add(btnConnect);
		
	}
		return first;
}
	
	/**
	 * this panel visible after the connection
	 * there is here diconnect button for disconnect the server
	 * @return
	 */
	public JPanel sec()
	{
		forswitchPanels.setSize(300,300);
		if (sec == null) {
			sec = new JPanel();
			sec.setLayout(null);
		}
		JButton btnNewButton = new JButton("disConnect");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				DBmethods.disconnectAll();
				es.sendToAllClients("disconnect");
				System.exit(0);
			}
		});
		btnNewButton.setBounds(75, 70, 140, 23);
		sec.add(btnNewButton);
		return sec;
	}
	
	
	public static void main(String[] args) {
		 new GUI_EchoServer();
		}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	
		
	}

	@Override
	/**
	 * calling when we close the server frame 
	 * we closing all the clients connection
	 */
	public void windowClosing(WindowEvent arg0) {
		DBmethods.disconnectAll();
		es.sendToAllClients("disconnect");
		System.exit(0);
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
}
